//
//  textviewMusic.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 周莹. All rights reserved.
//

#import "textviewMusic.h"
#import "UIView+Extension.h"
@interface textviewMusic ()<UITableViewDelegate>

@property(nonatomic,weak)UILabel  *lacehoderLabel;

@end



@implementation textviewMusic
-(id)initWithFrame:(CGRect)frame{

    self = [super  initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor   clearColor];
        //添加一个显示提醒文字的label(显示占位文字的label)
        
        UILabel *placehoderLabel = [[UILabel  alloc] init];
        //换行
        placehoderLabel.numberOfLines = 0;
        placehoderLabel.backgroundColor = [UIColor  clearColor];
        [self  addSubview:placehoderLabel];
        self.lacehoderLabel = placehoderLabel;
        //设置默认的占位文字的颜色
        self.placehoderColor = [UIColor  lightGrayColor];
        //设置默认字体
        self.font = [UIFont  systemFontOfSize:14];
        
        //监听内部文字的改变
        [[NSNotificationCenter  defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextFieldTextDidChangeNotification object:self];
        
        
        
    }

    return self;

}
//监听文字的改变
-(void)textDidChange{
//当长度部位0的时候隐藏
    self.lacehoderLabel.hidden = (self.text.length !=0);

}

-(void)setText:(NSString *)text{
    [super  setText:text];
    [self  textDidChange];



}

//#warning 如果是copy策略，setter最好这么写

-(void)setPlacehoder:(NSString *)placehoder{

    _placehoder = [placehoder copy];
    //设置文字
    self.lacehoderLabel.text = placehoder;
    // 重新计算子控件的fame
    [self setNeedsLayout];



}


-(void)setPlacehoderColor:(UIColor *)placehoderColor{

   _placehoderColor = placehoderColor;
    //设置颜色
    self.lacehoderLabel.textColor = placehoderColor;

}


-(void)setFont:(UIFont *)font{

    [super  setFont:font];
    self.lacehoderLabel.font = font;
    //重新计算子控件的fame
    [self  setNeedsLayout];


}
    //根据文字计算label高度
+(CGFloat)calculateHeightwithString:(NSString *)text{

    CGRect  rect =  [text  boundingRectWithSize:CGSizeMake([UIScreen  mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont  systemFontOfSize:17.0f]} context:nil];
    
    return rect.size.height;


}


-(void)layoutSubviews  {
    [super  layoutSubviews];
    
    self.lacehoderLabel.y = 8;
    self.lacehoderLabel.x =5;
    self.lacehoderLabel.width = self.width - 2*self.lacehoderLabel.x;
 

   
 
}







@end
